<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生id" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学生id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生账号" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          :maxlength="11"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属学校" prop="schoolId">
        <el-select v-model="queryParams.schoolId" filterable @change="queryChangeSchoolSelect()" placeholder="请选择学校">
          <el-option
            v-for="school in schoolList"
            :key="school.id"
            :label="school.name"
            :value="school.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属学院" prop="collegeId">
        <el-select v-model="queryParams.collegeId" filterable @change="queryChangeCollegeSelect()" placeholder="请选择学院">
          <el-option
            v-for="college in showCollegeList"
            :key="college.id"
            :label="college.name"
            :value="college.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属专业" prop="specialtyId">
        <el-select v-model="queryParams.specialtyId" filterable placeholder="请选择专业">
          <el-option
            v-for="specialty in showSpecialtyList"
            :key="specialty.id"
            :label="specialty.name"
            :value="specialty.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-select v-model="queryParams.grade" filterable placeholder="请选择年级">
          <el-option
            v-for="dict in dict.type.forum_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['forum:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['forum:student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['forum:student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['forum:student:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生id" fixed="left" width="55" align="center" show-overflow-tooltip prop="studentId" />
      <el-table-column label="账号名" align="center" show-overflow-tooltip prop="studentName" />
      <el-table-column label="密码" align="center" show-overflow-tooltip prop="password" />
      <el-table-column label="学生昵称" align="center" show-overflow-tooltip prop="nickName" />
      <el-table-column label="性别" align="center" show-overflow-tooltip prop="sex" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" show-overflow-tooltip prop="phonenumber" />
      <el-table-column label="邮箱" align="center" show-overflow-tooltip prop="email" />
      <el-table-column label="头像地址" align="center" show-overflow-tooltip prop="avatar" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="所属学校" align="center" show-overflow-tooltip prop="schoolId" >
        <template slot-scope="scope">
          {{ getSchoolNameForId(scope.row.schoolId) }}
        </template>
      </el-table-column>
      <el-table-column label="所属学院" align="center" show-overflow-tooltip prop="collegeId" >
        <template slot-scope="scope">
          {{ getCollegeNameForId(scope.row.collegeId) }}
        </template>
      </el-table-column>
      <el-table-column label="所属专业" align="center" show-overflow-tooltip prop="specialtyId" >
        <template slot-scope="scope">
          {{ getSpecialtyNameForId(scope.row.specialtyId) }}
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" show-overflow-tooltip prop="grade" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.forum_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="center" show-overflow-tooltip prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="最后登录IP" align="center" show-overflow-tooltip prop="loginIp" />
      <el-table-column label="最后登录时间" align="center" show-overflow-tooltip prop="loginDate" >
        <template slot-scope="scope">
          {{ formData(scope.row.loginDate) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['forum:student:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['forum:student:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleResetPassword(scope.row)"
            v-hasPermi="['forum:student:resetPwd']"
          >修改密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" @close="cancel()" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入账号名"></el-input>
        </el-form-item>
        <el-form-item label="密码"  v-if="form.studentId == undefined" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称">
          </el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号" :maxlength="11"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <!-- <el-input v-model="form.avatar" placeholder="请输入头像"></el-input> -->
          <image-upload v-model="form.avatar" :limit="1"/>
        </el-form-item>
        <el-form-item label="学校" prop="schoolId">
          <el-select v-model="form.schoolId" filterable @change="changeSchoolSelect()" placeholder="请选择学校">
            <el-option
              v-for="school in schoolList"
              :key="school.id"
              :label="school.name"
              :value="school.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学院" prop="collegeId">
          <el-select v-model="form.collegeId" filterable @change="changeCollegeSelect()" placeholder="请选择学院">
            <el-option
              v-for="college in showCollegeList"
              :key="college.id"
              :label="college.name"
              :value="college.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业" prop="specialtyId">
          <el-select v-model="form.specialtyId" filterable placeholder="请选择专业">
            <el-option
              v-for="specialty in showSpecialtyList"
              :key="specialty.id"
              :label="specialty.name"
              :value="specialty.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-select v-model="form.grade" filterable placeholder="请选择年级">
            <el-option
              v-for="dict in dict.type.forum_grade"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改学生密码对话框 -->
    <el-dialog :title="title" :visible.sync="passOpen" @close="cancel()" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--  -->
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent, updateStudentPassword } from "@/api/forum/student";
import {regListSchool} from '@/api/forum/school';
import {regListCollege} from '@/api/forum/college';
import {regListSpecialty} from '@/api/forum/specialty';
import {parseTime} from '@/utils/ruoyi.js';

export default {
  name: "Student",
  dicts: ['forum_grade','sys_normal_disable','sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      schoolList:[],
      collegeList:[],
      showCollegeList:[],
      specialtyList:[],
      showSpecialtyList:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生管理表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示修改密码弹出层
      passOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        schoolId: null,
        collegeId: null,
        specialtyId: null,
        grade: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentName: [
          { required: true, message: "账号名不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        phonenumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        schoolId: [
          { required: true, message: "学校id不能为空", trigger: "blur" }
        ],
        collegeId: [
          { required: true, message: "学院id不能为空", trigger: "blur" }
        ],
        specialtyId: [
          { required: true, message: "专业id不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "年级不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getSchoolList();
    this.getCollegeList();
    this.getSpecialtyList();
    this.getList();
  },
  methods: {
    //格式化时间
    formData(data){
      return parseTime(new Date(data));
    },
    //获取学校名字通过id
    getSchoolNameForId(schoolId){
      for(var i=0;i<this.schoolList.length;i++){
        if(schoolId==this.schoolList[i].id){
          return this.schoolList[i].name;
        }
      }
    },
    //获取学院名字通过id
    getCollegeNameForId(collegeId){
      for(var i=0;i<this.collegeList.length;i++){
        if(collegeId==this.collegeList[i].id){
          return this.collegeList[i].name;
        }
      }
    },
    //获取专业名字通过id
    getSpecialtyNameForId(specialtyId){
      for(var i=0;i<this.specialtyList.length;i++){
        if(specialtyId==this.specialtyList[i].id){
          return this.specialtyList[i].name;
        }
      }
    },
    //查询时点击学校按钮时筛选学院信息
    queryChangeSchoolSelect(){
      this.queryParams.collegeId=null;
      this.queryChangeCollegeSelect();
      this.getCollegeListForSchoolId(this.queryParams.schoolId);
    },
    //查询时点击学院按钮时筛选专业信息
    queryChangeCollegeSelect(){
      this.queryParams.specialtyId=null;
      // console.log("选择了："+this.form.collegeId)
      this.getSpecialtyListForCollegeId(this.queryParams.collegeId);
    },
    //点击学校按钮时筛选学院信息
    changeSchoolSelect(){
      this.form.collegeId=null;
      this.changeCollegeSelect();
      this.getCollegeListForSchoolId(this.form.schoolId);
    },
    //点击学院按钮时筛选专业信息
    changeCollegeSelect(){
      this.form.specialtyId=null;
      this.getSpecialtyListForCollegeId(this.form.collegeId);
    },
    /** 根据学院专业管理列表 */
    getSpecialtyListForCollegeId(collegeId) {
      var tempSpecialtyList = [];
      for(var i=0;i<this.specialtyList.length;i++){
        if(collegeId===this.specialtyList[i].collegeId){
          tempSpecialtyList.push(this.specialtyList[i]);
        }
      }
      this.showSpecialtyList=tempSpecialtyList;
    },
    /** 查询专业管理列表 */
    getSpecialtyList() {
      this.loading = true;
      regListSpecialty().then(response => {
        this.specialtyList = response.rows;
        this.showSpecialtyList = response.rows;
        this.loading = false; 
      });
    },
    /** 根据学校筛选学院管理列表 */
    getCollegeListForSchoolId(schoolId) {
      var tempCollegeList = [];
      for(var i=0;i<this.collegeList.length;i++){
        if(schoolId===this.collegeList[i].schoolId){
          tempCollegeList.push(this.collegeList[i]);
        }
      }
      this.showCollegeList=tempCollegeList;
    },
    /** 查询学院管理列表 */
    getCollegeList() {
      this.loading = true;
      regListCollege().then(response => {
        this.collegeList = response.rows;
        this.showCollegeList = response.rows;
        this.loading = false;
      });
    },
    /** 查询学校管理列表 */
    getSchoolList() {
      this.loading = true;
      regListSchool().then(response => {
        this.schoolList = response.rows;
        this.loading = false;
      });
    },
    /** 查询学生管理列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.passOpen = false;
      this.reset();
      if(this.queryParams.schoolId!=null&&this.queryParams.schoolId!=''){
        this.getCollegeListForSchoolId(this.queryParams.schoolId);
        // console.log("存在学校id,对学院展示列表进行筛选")
        if(this.queryParams.collegeId!=null&&this.queryParams.collegeId!=''){
          this.getSpecialtyListForCollegeId(this.queryParams.collegeId);
          // console.log("存在学院id,对专业展示列表进行筛选")
        }else{
          this.showSpecialtyList = null;
        }
      }else{
        this.showCollegeList = null;
        this.showSpecialtyList = null;
      }
      
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        schoolId: null,
        collegeId: null,
        specialtyId: null,
        grade: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.studentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.studentId || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.form.password = null;
        this.open = true;
        this.title = "修改学生管理";
        this.getCollegeListForSchoolId(this.form.schoolId);
        this.getSpecialtyListForCollegeId(this.form.collegeId);
      });
    },
    /** 修改密码按钮操作 */
    handleResetPassword(row){
      this.reset();
      const id = row.studentId || this.ids;
      getStudent(id).then(response => {
        this.form.studentId = response.data.studentId;
        this.form.studentName = response.data.studentName;
        this.passOpen = true;
        this.title = "修改"+this.form.studentName+"学生密码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.studentId != null) {
            if(this.form.password!=null && this.form.password != ""){
              updateStudentPassword(this.form).then(response => {
                this.$modal.msgSuccess("修改密码成功");
                this.passOpen = false;
                this.getList();
              });
            }else{
              updateStudent(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.studentId || this.ids;
      this.$modal.confirm('是否确认删除学生管理编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('forum/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
